from utils import log_util
from ssh.terminal import *


cmd_exit = 'exit'


def exec_cmd(cmd: str):
    try:
        if cmd == '':
            pass

        elif cmd == 'help' or cmd == '?':
            print('\033[32m\n输入 %s 退出' % cmd_exit)

        elif cmd == cmd_exit:
            # 退出指令
            exit(0)

        else:
            tg.batchExec(cmd)

    except Exception as ex:
        logging.error(ex, exc_info=True)
        exit(0)


if __name__ == '__main__':
    log_util.init_logging()

    # 需要连接的主机集合
    hosts = [Host('xiaoma1', username='root', password='root'),
             # Host('xiaoma2', username='xiaoma', password='xiaoma')
             ]

    tg = TerminalGroup(hosts)
    tg.connect()

    while True:
        cmd = input().strip()
        exec_cmd(cmd)
